package com.google.android.exoplayer2.upstream;

import android.net.Uri;
import androidx.annotation.Nullable;
import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
import o.alf;

public final class FileDataSource extends alf {
    @Nullable
    private Uri ˋ;
    @Nullable
    private RandomAccessFile ˎ;
    private boolean ˏ;
    private long ॱ;

    public static class FileDataSourceException extends IOException {
        public FileDataSourceException(IOException iOException) {
            super(iOException);
        }
    }

    public FileDataSource() {
        super(false);
    }

    public long ˋ(DataSpec dataSpec) throws FileDataSourceException {
        try {
            this.ˋ = dataSpec.ˎ;
            ˊ(dataSpec);
            this.ˎ = new RandomAccessFile(dataSpec.ˎ.getPath(), "r");
            this.ˎ.seek(dataSpec.ॱॱ);
            this.ॱ = dataSpec.ᐝ == -1 ? this.ˎ.length() - dataSpec.ॱॱ : dataSpec.ᐝ;
            if (this.ॱ < 0) {
                throw new EOFException();
            }
            this.ˏ = true;
            ˎ(dataSpec);
            return this.ॱ;
        } catch (IOException e) {
            throw new FileDataSourceException(e);
        }
    }

    public int ˋ(byte[] bArr, int i, int i2) throws FileDataSourceException {
        if (i2 == 0) {
            return 0;
        }
        if (this.ॱ == 0) {
            return -1;
        }
        try {
            int read = this.ˎ.read(bArr, i, (int) Math.min(this.ॱ, (long) i2));
            if (read <= 0) {
                return read;
            }
            this.ॱ -= (long) read;
            ˊ(read);
            return read;
        } catch (IOException e) {
            throw new FileDataSourceException(e);
        }
    }

    @Nullable
    public Uri ˊ() {
        return this.ˋ;
    }

    public void ॱ() throws FileDataSourceException {
        this.ˋ = null;
        try {
            if (this.ˎ != null) {
                this.ˎ.close();
            }
            this.ˎ = null;
            if (this.ˏ) {
                this.ˏ = false;
                ˏ();
            }
        } catch (IOException e) {
            throw new FileDataSourceException(e);
        } catch (Throwable th) {
            this.ˎ = null;
            if (this.ˏ) {
                this.ˏ = false;
                ˏ();
            }
        }
    }
}
